/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-explicit-any */

const { contextBridge, ipcRenderer } = require('electron')
const electronHandler = {
  ipcRenderer: {
    setStoreValue: (key, value) => {
      ipcRenderer.send('setStore', key, value)
    },

    getStoreValue(key) {
      const resp = ipcRenderer.sendSync('getStore', key)
      return resp
    },

    getStoreAllValue(key) {
      const resp = ipcRenderer.sendSync('getAllStore', key)
      return resp
    },

    removeStoreValue(key) {
      ipcRenderer.send('removeStore', key)
    },

    clearStoreValue() {
      ipcRenderer.send('clearStore')
    },

    onStoreChange: (callback) => {
      ipcRenderer.on('storeChange', (_event, newValue, oldValue) => callback(newValue, oldValue))
    },

    addFile(arrayBuffer, filename) {
      const fullpath = ipcRenderer.sendSync('addFile', arrayBuffer, filename)
      return fullpath
    },
    readFiles(path) {
      const data = ipcRenderer.sendSync('readFiles', path)
      return data
    },
    chooseFiles(fileTypes) {
      const paths = ipcRenderer.sendSync('chooseFiles', fileTypes)
      return paths
    },
    removeFiles(filepath) {
      ipcRenderer.send('removeFiles', filepath)
    },

    serialNumber() {
      return ipcRenderer.invoke('serialNumber')
    },

    permissions(mediaType) {
      return ipcRenderer.invoke('permissions', mediaType)
    },
    openSetting(mediaType) {
      ipcRenderer.send('openSetting', mediaType)
    },

    ping(hostname) {
      return ipcRenderer.invoke('ping', hostname)
    },
    //软件退出前监听
    close: (callback) => {
      ipcRenderer.on('close', (_event) => callback())
    },
    confirmClose: () => {
      ipcRenderer.send('confirmClose')
    },

    onFs: () => {
      return ipcRenderer.invoke('fsPermissions')
    },
  },
}
contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron,
  // 除函数之外，我们也可以暴露变量
})
contextBridge.exposeInMainWorld('electron', electronHandler)
